package com.reggie.exception;


import com.reggie.common.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

@Slf4j
@RestControllerAdvice
public class SqlExceptionHandler {

    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R<String> exceptionHandler(SQLIntegrityConstraintViolationException e){
        log.error(e.getMessage());

        if(e.getMessage().contains("Duplicate entry")){
            String[] split = e.getMessage().split(" ");
            String msg = split[2] + "已存在";
            return R.error(msg);
        }
        return R.error("未知错误");
    }

    @ExceptionHandler(BusinessException.class)
    public R<String> exceptionHandler(BusinessException e){
        log.error(e.getMessage());

        return R.error(e.getMessage());
    }

    @ExceptionHandler(SystemException.class)
    public R<String> exceptionHandler(SystemException e){
        log.error(e.getMessage());

        return R.error(e.getMessage());
    }
}
